package org.bozo.im.activity;

import org.bozo.im.common.MessageContent;
import org.bozo.im.common.MessageType;
import org.bozo.im.common.types.AccountDesc;
import org.bozo.im.common.types.Protocol;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class ServiceMessenger {

	private Messenger mService;
	private Messenger mReplyTo;
	
	public ServiceMessenger(IBinder pService, Messenger pReplyTo) {
		mService = new Messenger(pService);
		mReplyTo = pReplyTo;
	}
	
	public void registerClient() {
		registerClient(mReplyTo);
	}
	
	public void registerClient(Messenger pReplyTo) {
		Message lMessage = Message.obtain();
		lMessage.what = MessageType.REGISTER_CLIENT;
		lMessage.replyTo = pReplyTo;
		
		sendMessage(lMessage);
	}

	public void unregisterClient() {
		unregisterClient(mReplyTo);
	}
	
	public void unregisterClient(Messenger pReplyTo) {
		Message lMessage = Message.obtain();
		lMessage.what = MessageType.UNREGISTER_CLIENT;
		lMessage.replyTo = pReplyTo;
		
		sendMessage(lMessage);
	}
	
	public void addAccount(AccountDesc pAccount) {
		addAccount(pAccount, mReplyTo);
	}
	
	public void addAccount(AccountDesc pAccount, Messenger pReplyTo) {
		Bundle lData = new Bundle();
		lData.putParcelable(MessageContent.ACCOUNT, pAccount);

		Message lMessage = Message.obtain();
		lMessage.what = MessageType.ADD_ACCOUNT;
		lMessage.replyTo = pReplyTo;
		lMessage.setData(lData);

		sendMessage(lMessage);
	}
	
	public void getRoster() {
		getRoster(mReplyTo);
	}
	
	public void getRoster(Messenger pReplyTo) {
		Message lMessage = Message.obtain();
		lMessage.what = MessageType.GET_ROSTER;
		lMessage.replyTo = pReplyTo;

		sendMessage(lMessage);
	}
	
	public void getProtocols() {
		getProtocols(mReplyTo);
	}
	
	public void getProtocols(Messenger pReplyTo) {
		Message lMessage = Message.obtain();
		lMessage.what = MessageType.GET_PROTOCOLS;
		lMessage.replyTo = pReplyTo;

		sendMessage(lMessage);
	}
	
	public void getProtocolRequirements(Protocol pProtocol) {
		getProtocolRequirements(pProtocol, mReplyTo);
	}
	
	public void getProtocolRequirements(Protocol pProtocol, Messenger pReplyTo) {
		Bundle lData = new Bundle();
		lData.putParcelable(MessageContent.PROTOCOL, pProtocol);
		
		Message lMessage = Message.obtain();
		lMessage.what = MessageType.GET_PROTOCOL_REQUIREMENT;
		lMessage.replyTo = pReplyTo;
		lMessage.setData(lData);

		sendMessage(lMessage);
	}
	
	private void sendMessage(Message pMessage) {
		try {
			mService.send(pMessage);
		} catch (RemoteException e) {
			// ---------------------------------------------------------------------------------------
			Log.e(Constants.ACTIVITY_TAG, "org.bozo.im.activity.ServiceMessenger.sendMessage ---------- RemoteException: ", e);
			// ---------------------------------------------------------------------------------------
		}
	}


}
